﻿using com.kylin.sddemo.info;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace com.kylin.sddemo.utils
{
    public class EnumUtils
    {
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="en"></param>
        ///// <returns></returns>
        public static string GetEnumName(Enum en)
        {
            Type type = en.GetType();
            FieldInfo[] fieldinfos = type.GetFields();
            var EnumName = "";
            foreach (FieldInfo field in fieldinfos)
            {
                if (field.FieldType.IsEnum)
                {
                    var Value = (int)field.GetValue(field);
                    if (Value == Convert.ToInt32(en))
                    {
                        EnumName = field.Name;
                        return EnumName;
                    }
                }
            }
            return EnumName;
        }

        /// <summary>
        /// 获取枚举项描述信息 例如GetEnumDesc(Days.Sunday)
        /// </summary>
        /// <param name="en">枚举项 如Days.Sunday</param>
        /// <returns></returns>
        public static string GetEnumDesc(Enum en)
        {
            Type type = en.GetType();
            MemberInfo[] memInfo = type.GetMember(en.ToString());
            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if (attrs != null && attrs.Length > 0)
                    return ((DescriptionAttribute)attrs[0]).Description;
            }
            return en.ToString();
        }
        ///<summary>
        /// 获取枚举项+描述
        ///</summary>
        ///<param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
        ///<returns>键值对</returns>
        public static Dictionary<string, string> GetEnumItemDesc(Type enumType)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            FieldInfo[] fieldinfos = enumType.GetFields();
            foreach (FieldInfo field in fieldinfos)
            {
                if (field.FieldType.IsEnum)
                {
                    Object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                    dic.Add(field.Name, ((DescriptionAttribute)objs[0]).Description);
                }
            }
            return dic;
        }

        ///<summary>
        /// 获取枚举值+描述
        ///</summary>
        ///<param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)</param>
        ///<returns>键值对</returns>
        public static Dictionary<string, string> GetEnumItemValueDesc(Type enumType)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            Type typeDescription = typeof(DescriptionAttribute);
            FieldInfo[] fields = enumType.GetFields();
            foreach (FieldInfo field in fields)
            {
                if (field.FieldType.IsEnum)
                {
                    string strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();
                    object[] arr = field.GetCustomAttributes(typeDescription, true);
                    string strText;
                    if (arr.Length > 0)
                    {
                        DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                        strText = aa.Description;
                    }
                    else
                    {
                        strText = field.Name;
                    }
                    dic.Add(strValue, strText);
                }
            }
            return dic;
        }

        /// <summary>
        /// 枚举转列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns>IList<EnumEntity></returns>
        public static IList<EnumEntity> EnumToList<T>()
        {
            IList<EnumEntity> list = new List<EnumEntity>();
            foreach (var item in Enum.GetValues(typeof(T)))
            {
                EnumEntity enumEntity = new EnumEntity();
                Object[] objArr = item.GetType().GetField(item.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute descAttr = objArr[0] as DescriptionAttribute;
                    enumEntity.Text = descAttr.Description;
                }
                enumEntity.Value = Convert.ToInt32(item);
                enumEntity.Name = item.ToString();
                list.Add(enumEntity);
            }
            return list;
        }
    }
}
